package com.xiaoshuidi.cloud.framework.filings.core.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.CentralizeHouseCheckBo;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.EnterpriseHousingInquiryBo;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.FilesUploadBo;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.InventoryCheckBo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.util.ObjectUtils;

import javax.sound.sampled.AudioPermission;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Feng
 * @date 2023/5/5
 */
public class SignUtil {

    /**
     * 生成签名
     *
     * @param obj          对象
     * @param timestamp    时间戳
     * @param isGetRequest 是否get请求
     * @return 签名
     */
    public static String generateSign(Object obj, long timestamp, boolean isGetRequest, String appKey) {
        return encryptMd5(preGenerateSign(obj, timestamp, isGetRequest, appKey));
    }

    /**
     * 待签名字符串
     *
     * @param obj       对象
     * @param timestamp 时间戳
     * @return 待加密字符串
     */
    private static String preGenerateSign(Object obj, long timestamp, boolean isGetRequest, String appKey) {
        String preSign = "appKey" + appKey +
                assiiObject(obj, isGetRequest) +
                "timestamp" + timestamp;
        return preSign.toUpperCase();
    }

    /**
     * 将对象进行ASSII升序排序
     *
     * @param obj 对象
     * @return ASSII升序排序后字符串
     */
    private static String assiiObject(Object obj, boolean isGetRequest) {
        String json = JSON.toJSONString(obj);
        JSONObject jsonObject = JSON.parseObject(json);
        Set<String> keys = jsonObject.keySet();
        List<String> keyArrs = new ArrayList<>();
        keyArrs.addAll(keys);
        List<String> collect = keyArrs.stream().sorted().collect(Collectors.toList());
        if (isGetRequest) {
            StringBuilder sb = new StringBuilder();
            for (String key : collect) {
                sb.append(key).append(jsonObject.get(key));
            }
            return sb.toString();
        }

        return JSON.toJSONString(obj);
    }

    /**
     * md5加密
     *
     * @param str 待加密字符串
     * @return 加密后md5字符串
     */
    private static String encryptMd5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
            }
            return sb.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
